﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace lnxsj.web.Recruitment.Areas.manage.Controllers
{
    using Models;
    using Classes;
    using Filters;
    [Authorize]
    [InitializeSimpleMembership]
    public class EmployController : Controller
    {

        private RecrunitmenutDB db = new RecrunitmenutDB();

        public ActionResult Index()

        {

            ViewData.Model = db.Employs.Where(d => d.处理 == 处理Type.未读).OrderByDescending(d => d.Id).ToList();
            return View();
        }
        public ActionResult GDIndex()
        {
            ViewData.Model = db.Employs.Where(d => d.处理 == 处理Type.归档).OrderByDescending(d => d.Id);
            return View();
        }
        public ActionResult DelIndex()
        {
            ViewData.Model = db.Employs.Where(d => d.处理 == 处理Type.删除).OrderByDescending(d => d.Id);
            return View();
        }
        public ActionResult Details(int Id)
        {
            ViewData.Model = db.Employs.Find(Id);
            return View();
        }
        public ActionResult delBack(int Id) {
            string info = "";
            try
            {
                var employ = db.Employs.Find(Id);
                employ.处理 = 处理Type.未读;
                db.SaveChanges();
            }
            catch (Exception e) {
                info = e.Message;
            }

            if (Request.IsAjaxRequest())
            {
                if (info == "")
                {
                    return myAjaxResult.success();
                }
                else
                {
                    return myAjaxResult.error("该记录已经不存在。或其它错误");
                }
            }
            else
            {
                return RedirectToAction("DelIndex");
            }            
        }
        public ActionResult del(int Id,string parentAction,Boolean TrueDel = false)
        {
            string info = "";

                try
                {
                    var employ = db.Employs.Find(Id);
                    if (TrueDel)
                    {
                        db.Employs.Remove(employ);
                    }
                    else
                    {
                        employ.处理 = 处理Type.删除;
                    }
                    db.SaveChanges();
                    
                }
                catch(Exception e)
                {
                    info=e.Message;
                }

                if (Request.IsAjaxRequest())
                {
                    if (info == "")
                    {
                        return myAjaxResult.success();
                    }
                    else
                    {
                        return myAjaxResult.error("该记录已经不存在。或其它错误");
                    }
                }
                else
                {
                    return RedirectToAction(parentAction);
                }
            

        }
        public ActionResult submit(int id,string parentAction)
        {
            Employ te = db.Employs.Find(id);
            te.处理 = 处理Type.归档; //标记已归档
            db.SaveChanges();
            /*
            Employ e = new Employ();
            var teType = te.GetType();
            var eType = e.GetType();
            foreach (var x in teType.GetProperties())
            {
                string FieldName = x.Name;
                if (FieldName == "Id") continue;

                object v = teType.GetProperty(x.Name).GetValue(te, null);
                object _v = Convert.ChangeType(v, eType.GetProperty(FieldName).PropertyType);

                var _field = eType.GetProperty(FieldName);

                if (_field != null)
                {
                    _field.SetValue(e, _v);
                }

                

            }
            db.Employs.Add(e);
             * */

            if (Request.IsAjaxRequest())
            {


               return myAjaxResult.success();


            }
            else
            {
                ViewBag.parentAction = parentAction;
                return RedirectToAction("index");
            }
            
        }

    }
}
